<template>
    <cl-drawer
        ref="drawer"
        :props="{
			title: '选择深转标签',
			size: '620px'
		}"
        @close="reset"
        @confirm="submit"
        show-confirm
		confirm-text="提交"
    >
        <el-form ref="form" label-position="top" :model="form" :disabled="saving">
            <el-form-item v-for="group, group_id in this.tags" :key="group_id" :label="group.group_name">
                <el-radio v-for="tag, tag_id in group.tag" v-model="form.tag_id" :key="tag_id" :label="tag.id">{{ tag.name }}</el-radio>
            </el-form-item>
            <el-form-item>
                <span style="cursor: pointer; text-decoration: underline; color: #156dff;" @click="refreshTag">刷新标签</span>
            </el-form-item>
        </el-form>
    </cl-drawer>
</template>
<script>
export default {
    data() {
        return {
            saving: false,
            form: {
                tag_id: ''
            },
            tags: [],
            corpid: '',
            origin_tag_id: ''
        }
    },
    methods: {
        open(data) {
            this.reset()
            this.tags = JSON.parse(data.wxk_tag_group)
            this.corpid = data.corpid
            this.form.tag_id = this.origin_tag_id = data.intention_tag_id
            this.$refs.drawer.create()
        },
        async submit() {
            this.saving = true

            const corpid = this.corpid
            const tag_id = this.form.tag_id
            if (tag_id !== "" && tag_id !== this.origin_tag_id) {
                await this.$service.workwechat.setIntentionTag({corpid, tag_id})
                this.$emit("confirm", false);
            }
            this.reset()
            this.close()
        },
        close() {
            this.$refs.drawer.close();
        },
        reset() {
            this.saving = false;
            this.form = {
                tag_id: ''
            };
            this.tags = [];
        },
        async refreshTag() {
            this.saving = true
            const corpid = this.corpid
            const tags = await this.$service.workwechat.refreshCorpTags({ corpid })
            console.log(tags, 'refreshTag')
            this.tags = tags
            this.saving = false
        }
    }
}
</script>
<style>

</style>